/*
 * Copyright (c) 2020 hebeiyiliuba.com
 * All rights reserved.
 *
 */
package org.jeecg.modules.third.jg.report.freight.information.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.base.service.BaseCommonService;
import org.jeecg.modules.third.jg.report.freight.information.constant.AhReportConstant;
import org.jeecg.modules.third.jg.report.freight.information.constant.enums.ReportDocTypeEnum;
import org.jeecg.modules.third.jg.report.freight.information.dto.AppInfoDTO;
import org.jeecg.modules.third.jg.report.freight.information.model.ExchangeModel;
import org.jeecg.modules.third.jg.report.freight.information.model.R;
import org.jeecg.modules.third.jg.report.freight.information.util.WebUtils;
import org.jeecg.modules.third.jg.report.freight.information.util.sm.SM2Util;
import org.jeecg.modules.third.jg.report.freight.information.util.sm.SM4Util;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.utils.HyCfgUtil;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 安徽省平台上报服务
 * @author duanlikao
 * @created 2020/12/31
 */
@Service
@Slf4j
public class AhReportPlatformService {

    @Resource
    private  RedisUtil redisUtil;

    @Resource
    private BaseCommonService baseCommonService;

    private String reportPlatformDomain;

    private String username;

    private String password;

    private String publickey;

    private String appId;

    private String appSecurity;

    private String appSenderCode;

    private String appDebugSystem;

    public AppInfoDTO getAppInfo() {
        return AppInfoDTO.builder()
                .appId(this.appId)
                .appSecurity(this.appSecurity)
                .enterpriseSenderCode(this.appSenderCode)
                .debugSystem(this.appDebugSystem)
                .build();
    }


    public void setReportInfo(String reportPlatformDomain,
                              String username,
                              String password,
                              String publickey,
                              String appId,
                              String appSecurity,
                              String appSenderCode,
                              String appDebugSystem) {
        //联调状态的App需要设置debugSystem
        if ("0".equals(appDebugSystem)) {
            appDebugSystem = "debugSystem";
        }

        this.reportPlatformDomain = reportPlatformDomain;
        this.username = username;
        this.password = password;
        this.publickey = publickey;
        this.appId = appId;
        this.appSecurity = appSecurity;
        this.appSenderCode = appSenderCode;
        this.appDebugSystem = appDebugSystem;
    }


    private String getLoginToken() throws Exception {
        return "";
    }

    /**
     * 组装交换报文
     * @param reportDocTypeEnum 上报单据类型
     * @param jsonString 报文内容
     * @return
     */
    private ExchangeModel messageAssembly(ReportDocTypeEnum reportDocTypeEnum ,String jsonString) throws Exception {
        return null;
    }

    /**
     * 发送上报
     * @param reportDocTypeEnum
     * @param jsonString
     */
    public String sendReport(ReportDocTypeEnum reportDocTypeEnum ,String jsonString, String tag) throws Exception {
        String sendResult = StringUtils.EMPTY;
        try {
            String url = reportPlatformDomain + AhReportConstant.SEND_URL;
            ExchangeModel exchangeModel = messageAssembly(reportDocTypeEnum, jsonString);
            if (exchangeModel == null) {
                throw new JeecgBootException("交换报文内容错误");
            }
            //发送报文
            //log.info("业务日志报文记录 -> {}", JSONUtil.toJsonStr(exchangeModel));
            log.info("安徽监管发送报文 -> {} ->{} -> {}-> {}", url, reportDocTypeEnum.getMsg(),
                    exchangeModel.getRoot().getHeader().getMessageReferenceNumber() ,jsonString);
            sendResult = HttpUtil.post(url, JSONUtil.toJsonStr(exchangeModel));
            log.info("安徽监管回执报文 <- {}", sendResult);

            //保留电子运单1 2 3和资金流水报文号日志
            if (reportDocTypeEnum.getMsg().equals(ReportDocTypeEnum.REPORT_DOC_TYPE_WLHY_YD3.getMsg())
             || reportDocTypeEnum.getMsg().equals(ReportDocTypeEnum.REPORT_DOC_TYPE_WLHY_YD2.getMsg())
             || reportDocTypeEnum.getMsg().equals(ReportDocTypeEnum.REPORT_DOC_TYPE_WLHY_YD1.getMsg())
             || reportDocTypeEnum.getMsg().equals(ReportDocTypeEnum.REPORT_DOC_TYPE_WLHY_ZJ.getMsg())) {
                this.baseCommonService.addLog(reportDocTypeEnum.getMsg() + " - " + tag + " - " +
                        exchangeModel.getRoot().getHeader().getMessageReferenceNumber() + " - " + sendResult, CommonConstant.LOG_TYPE_4, CommonConstant.OPERATE_TYPE_2);
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("安徽监管上报数据异常 :{}", e.getMessage());
        }
        return sendResult;
    }

}
